package com.barbeque.mall.service;

import com.barbeque.mall.bean.advertise.PO.TopicDeletePo;
import com.barbeque.mall.bean.advertise.BO.TopicListBo;
import com.barbeque.mall.bean.advertise.MarketTopic;
import com.barbeque.mall.bean.advertise.MarketTopicExample;
import com.barbeque.mall.bean.advertise.VO.TopicUpdateVo;
import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketGoodsMapper;
import com.barbeque.mall.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bai
 * @since 2022/07/18 15:59
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
     * 显示专题管理列表的逻辑
     * @param topicListBo
     * @return
     */
    @Override
    public CommonData queryTopicList(TopicListBo topicListBo) {

        PageHelper.startPage(topicListBo.getPage(),topicListBo.getLimit());

        MarketTopicExample marketTopicExample = new MarketTopicExample();

        marketTopicExample.setOrderByClause(topicListBo.getSort());

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);

        return CommonData.data(marketTopicPageInfo);
    }

    /**
     * 添加一条专题数据的逻辑
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic insertTopicInfo(MarketTopic marketTopic) {

        //获取当前时间
        Date date = new Date(System.currentTimeMillis());

        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);

        // 插入数据库
        marketTopicMapper.insertSelective(marketTopic);

        marketTopic = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());



        return marketTopic;
    }

    /**
     * 根据id更新一条专题信息的逻辑
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic updateTopicInfo(MarketTopic marketTopic) {

        // 获取当前时间
        Date date = new Date(System.currentTimeMillis());
        marketTopic.setUpdateTime(date);

        // 更新
        marketTopicMapper.updateByPrimaryKeyWithBLOBs(marketTopic);

        // 查询更新后的内容
        marketTopic = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());

        return marketTopic;
    }

    /**
     * 根据id进行删除专题信息的逻辑
     * @param marketTopic
     */
    @Override
    public void deleteTopicInfo(MarketTopic marketTopic) {
        marketTopicMapper.deleteByPrimaryKey(marketTopic.getId());
    }

    /**
     * 显示要更改的专题信息的逻辑
     * @param id
     * @return
     */
    @Override
    public TopicUpdateVo queryTopicInfoById(Integer id) {
        TopicUpdateVo topicUpdateVo = new TopicUpdateVo();
        ArrayList<MarketGoods> marketGoods = new ArrayList<>();

        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        topicUpdateVo.setTopic(marketTopic);

        if(marketTopic.getGoods() != null){
            for (Integer goodsId : marketTopic.getGoods()) {
                marketGoods.add(marketGoodsMapper.selectByPrimaryKey(goodsId));
            }
            topicUpdateVo.setGoodsList(marketGoods);
        }

        return topicUpdateVo;
    }

    /**
     * 专题列表的模糊查询操作
     * @param topicListBo
     * @return
     */
    @Override
    public CommonData<MarketTopic> queryTopicListCondition(TopicListBo topicListBo) {
        PageHelper.startPage(topicListBo.getPage(),topicListBo.getLimit());

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();

        if(topicListBo.getTitle() != null){
            criteria.andTitleLike("%" + topicListBo.getTitle() + "%");
        }
        if(topicListBo.getSubtitle() != null){
            criteria.andSubtitleLike("%" + topicListBo.getSubtitle() + "%");
        }

        marketTopicExample.setOrderByClause(topicListBo.getSort());

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);

        return CommonData.data(marketTopicPageInfo);
    }

    /**
     * 批量删除的逻辑
     * @param
     */
    @Override
    public void deleteTopicInfoByIds(TopicDeletePo topicDeletePo) {
        Integer[] ids = topicDeletePo.getIds();
        for (Integer id : ids) {
            marketTopicMapper.deleteByPrimaryKey(id);
        }
    }
}
